package sample.util;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull;
import sample.SceneBuilder;

/**

 * @name      :Ctl

 * @Description: a static factory to trim some basic components

*/
public class Ctl {
    public static void ColLineSet(Pane pane, Line[] col, int part){
        for (int i = 0; i < col.length; i++) {
            col[i]=LineStroke(new Line());
            col[i].startXProperty().set(0);
            col[i].startYProperty().bind(pane.heightProperty().divide(part).multiply(i+1));
            col[i].endXProperty().bind(pane.widthProperty());
            col[i].endYProperty().bind(col[i].startYProperty());
        }
    }
    public static void RowLineSet(Pane pane, Line[] row,int part) {
        for (int i = 0; i < row.length; i++) {
            row[i]=LineStroke(new Line());
            row[i].startXProperty().bind(pane.widthProperty().divide(part).multiply(i+1));
            row[i].startYProperty().set(0);
            row[i].endXProperty().bind(row[i].startXProperty());
            row[i].endYProperty().bind(pane.heightProperty());
        }
    }
    private static Line LineStroke(Line line){
        return Ctl.LineProperty(line, Color.BLACK,line.getStrokeWidth());
    }
    public static Line LineProperty(@NotNull Line line, Color strokeColor, double width){
        line.setStroke(strokeColor);
        line.setStrokeWidth(width);
        return line;
    }
    public static Line LineProperty(double x1,double y1,double x2,double y2,Color strokeColor,double width){
        return Ctl.LineProperty(new Line(x1,y1,x2,y2),strokeColor,width);
    }
    /**
        @brief small middle large and self-defined
     */
    public static void Paint_Menu_Three_Type_Set(Menu m){
        m.getItems().addAll(new MenuItem("small"),new MenuItem("middle")
                ,new MenuItem("big"),new MenuItem("..."));
    }
    public static Menu Paint_Menu_Three_Type_Set(String text){
        Menu m=Ctl.MenuWithText(text);
        Ctl.Paint_Menu_Three_Type_Set(m);
        return m;
    }
    public static void MenuAddCommonItemWithText(Menu m,String...text){
        for(String s : text){
            m.getItems().add(new MenuItem(s));
        }
    }
    public static Menu MenuWithText(String text){
        Menu m=new Menu();
        m.setText(text);
        return m;
    }
    public static Stage NullStage(){
        return Ctl.MessageBox("Null", SceneBuilder.NullScene(),15);
    }
    public static Stage MessageBox(String msg, Scene scene, int duration){
        return new Alert(msg,duration,scene);
    }
    public static MenuItem NullMenuItemOperation(MenuItem mi){
        mi.setOnAction(Ctl.NullEventHandler());
        return mi;
    }
    public static EventHandler<ActionEvent> NullEventHandler(){
        return e->{
            Ctl.NullStage().show();
        };
    }
    public static ContextMenu HelpContextMenu(){
        Hyperlink link=new Hyperlink("http://hughnew.gitee.io/relax-box/");
        link.autosize();
        ContextMenu contextMenu=new ContextMenu();
        MenuItem mi=new MenuItem("help");
        mi.setGraphic(link);
        link.setContentDisplay(ContentDisplay.LEFT);
        contextMenu.setOnAction(e->{
            if(java.awt.Desktop.isDesktopSupported()){
                new Alert("java.awt.Desktop.getDesktop can't be solved",8).show();
    //            Desktop desktop=new java.awt.Desktop.getDesktop();
    //            try {
    //                desktop.browse(URI.create(link.getText()));
    //            } catch (IOException e) {
    //                new Alert("maybe there is no network");
    //            }
            }else{
                new Alert("local browser is not supported",5).show();
            }
        });
        contextMenu.getItems().add(mi);
        return contextMenu;
    }
}
